package com.george.caffeine.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.george.caffeine.dao.mybatis.OrderMapper;
import com.george.caffeine.entity.dao.OrderDAO;
import com.george.caffeine.service.OrderService;
import com.george.caffeine.constants.CacheConstant;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * @title: OrderServiceImpl.java
 * @description: 订单服务实现
 * @author: George
 * @date: 2023/3/20 15:20
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class OrderServiceImpl implements OrderService {

    private final RedisTemplate redisTemplate;

    private final OrderMapper orderMapper;


    /**
     * 批量新增订单
     *
     * @param orderDAOS
     * @return void
     * @author George
     * @date 2023/3/21 10:11
     */
    @Override
    public void insertBatch(List<OrderDAO> orderDAOS) {
        orderMapper.insertBatch(orderDAOS);
    }

    /**
     * 新增订单
     *
     * @param orderDAO
     * @return java.lang.Integer
     * @author George
     * @date 2023/3/21 10:06
     */
    @Override
    public Integer insertOrder(OrderDAO orderDAO) {
        return orderMapper.insert(orderDAO);
    }

    /**
     * 根据订单id获取订单信息
     *
     * @param id
     * @return com.george.caffeine.entity.dao.OrderDAO
     * @author George
     * @date 2023/3/20 15:19
     */
    @Cacheable(value = "order", key = "#id")
    @Override
    public OrderDAO getOrderById(String id) {
        String key = CacheConstant.ORDER + id;
        //先查询 Redis
        Object obj = redisTemplate.opsForValue().get(key);
        if (Objects.nonNull(obj)) {
            log.info("get data from redis");
            return (OrderDAO) obj;
        }
        // Redis没有则查询 DB
        log.info("get data from database");
        OrderDAO myOrder = orderMapper.selectOne(new LambdaQueryWrapper<OrderDAO>()
                .eq(OrderDAO::getId, id));
        redisTemplate.opsForValue().set(key, myOrder, 120, TimeUnit.SECONDS);
        return myOrder;
    }

    /**
     * 分页查询订单信息
     *
     * @param orderDAO
     * @param pageNum
     * @param pageSize
     * @return com.baomidou.mybatisplus.core.metadata.IPage<com.george.caffeine.entity.dao.OrderDAO>
     * @author George
     * @date 2023/3/21 9:49
     */
    @Override
    public IPage<OrderDAO> getOrderByPage(OrderDAO orderDAO, Integer pageNum, Integer pageSize) {
        IPage<OrderDAO> page = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<OrderDAO> queryWrapper = new LambdaQueryWrapper<>();
        orderMapper.selectPage(page,queryWrapper);
        return page;
    }

    /**
     * 更新订单信息
     *
     * @param order
     * @return com.george.caffeine.entity.dao.OrderDAO
     * @author George
     * @date 2023/3/20 15:20
     */
    @CachePut(cacheNames = "order", key = "#order.id")
    @Override
    public OrderDAO updateOrder(OrderDAO order) {
        log.info("update order data");
        orderMapper.updateById(order);
        //修改 Redis
        redisTemplate.opsForValue().set(CacheConstant.ORDER + order.getId(),
                order, 120, TimeUnit.SECONDS);
        return order;
    }

    @CacheEvict(cacheNames = "order", key = "#id")
    @Override
    public void deleteOrder(String id) {
        log.info("delete order");
        orderMapper.deleteById(id);
        redisTemplate.delete(CacheConstant.ORDER + id);
    }
}
